package com.controller;

import com.bean.*;
import com.service.BookingService;
import com.service.CdyOrderService;
import com.service.CommodityService;
import com.service.SalesService;
import com.util.PageAssistant;
import com.vo.SalesVo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("sales")
public class SalesController {

    @Autowired
    private SalesService salesService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private CdyOrderService cdyOrderService;
    @Autowired
    private BookingService bookingService;


    /**
     * 展示活动
     * @param session
     * @param model
     * @param sales
     * @param paSales
     * @return
     */
    @Transactional//**********************************************事务**************************************************
    @RequestMapping("querySales")
    public  String querySales(HttpSession session, Model model, Sales sales, PageAssistant<Sales> paSales) {
        System.out.println("querySales");
        System.out.println(sales);
        System.out.println(model);
        System.out.println("---------------------");

        HashMap<String, Object> map = new HashMap<>();
        map.put("start",paSales.getStart());
        map.put("size",paSales.getSize());

        paSales.setCount(salesService.count(map));
        paSales.setItems(salesService.query(map));

        model.addAttribute("paSales",paSales);
        System.out.println(paSales.getItems());
        return "page/sales/sealsList";
    }

    /**
     * 新增活动页
     * @param session
     * @param model
     * @param sales
     * @return
     */
    @Transactional
    @RequestMapping("saveSales")
    public  String saveSales(HttpSession session, Model model, Sales sales , Commodity commodity) {
        System.out.println("saveSales");
        System.out.println(sales);
        System.out.println(model);
        System.out.println("---------------------");
        //查询所有的商品信息
        List<Commodity> commodities = commodityService.find(commodity);
        model.addAttribute("commodities",commodities);
        return "page/sales/add";
    }

    /**
     * 存储活动
     * @param session
     * @param model
     * @param sales
     * @return
     */
    @Transactional
    @RequestMapping("save")
    public  String save(HttpSession session, Model model, SalesVo sales) {
        System.out.println("save");
        System.out.println(sales);
        System.out.println(model);
        System.out.println("---------------------");

        int num = salesService.save(sales.parse());
        System.out.println(num+"<><><><><><><><增加成功为1");
        return "redirect:/sales/querySales";
    }

    /**
     * 找到选中的修改活动
     * @param session
     * @param model
     * @param sales
     * @return
     */
    @RequestMapping("modifySales")
    public  String modifySales(HttpSession session, Model model, SalesVo sales,Commodity commodity) {
        System.out.println("modifySales");
        System.out.println(sales);
        System.out.println(model);
        System.out.println(commodity);
        System.out.println("---------------------");
        List<Sales> salesList = salesService.find(sales.parse());
        //查询所有的商品信息
        commodity.setId(null);
        List<Commodity> commodities = commodityService.find(commodity);
        System.out.println(commodities+"<><><><><查询到的所有货物");
        model.addAttribute("commodities",commodities);
        model.addAttribute("sales",salesList.get(0));
        return "page/sales/modifySeals";
    }

    /**
     * 修改活动
     * @param session
     * @param model
     * @param sales
     * @param commodity
     * @return
     */
    @Transactional
    @RequestMapping("modify")
    public  String modify(HttpSession session, Model model, SalesVo sales,Commodity commodity) {
        System.out.println("modify");
        System.out.println(sales);
        System.out.println(model);
        System.out.println(commodity);
        System.out.println("---------------------");
        int modify = salesService.modify(sales.parse());
        System.out.println("><><><><><><"+modify+"个修改成功<><><><><");
        return "redirect:/sales/querySales";
    }

    @Transactional
    @RequestMapping("deleteSales")
    public  String remove(HttpSession session, Model model, SalesVo sales,Commodity commodity) {
        System.out.println("remove");
        System.out.println(sales);
        System.out.println(model);
        System.out.println(commodity);
        System.out.println("---------------------");
        int remove = salesService.remove(sales.parse());
        System.out.println("><><><><><><"+remove+"个删除成功<><><><><");
        return "redirect:/sales/querySales";
    }

    @Transactional
    @RequestMapping("choose2")
    public String choose2(HttpSession session, Model model, SalesVo sales,
                         Commodity commodity, Booking booking, Seat seat,CdyOrder cdyOrder) {
        System.out.println("active choose");
        System.out.println(sales);
        System.out.println(model);
        System.out.println(cdyOrder);
        System.out.println(commodity);
        System.out.println(booking);
        System.out.println("---------------------");

        Sales parse = sales.parse();

        //活动与购票相关
        if (parse.getState() == 2){
            //查询订单相关
            List<Booking> bookings = bookingService.find(booking);
            Booking b = bookings.get(0);
            if (bookings.size() > 0){
                Date date = new Date();
                if (b.getOrderTime().getTime()+ 5*60*1000 < b.getTime().getTime()|| b.getState() == 2){
                    System.out.println("对不起,电影提前5分钟参与活动或者您的票已经参加过活动,或者没有该订单信息,我们不予办理!");
                    return "page/sales/false";
                }else{

                    //修改库存
                    commodity.setId(parse.getCommodityId());
                    //查询到要修改的commodity信息
                    List<Commodity> commodities = commodityService.find(commodity);
                    Commodity co = commodities.get(0);
                    int num =co.getStock() - parse.getNum();
                    System.out.println(commodity+"改库存前");
                    commodity.setStock(num);
                    System.out.println(commodity+"改库存后");
                    commodityService.modify(commodity);

                    //修改订单状态
                    booking.setState(2);
                    int modify = bookingService.modify(booking);
                    System.out.println("修改状态成功num="+modify);
                    //修改座位状态

                    return "page/sales/ok";
                }

            }else{
                System.out.println("对不起,电影提前5分钟参与活动或者您的票已经参加过活动,或者没有该订单信息,我们不予办理!");
                return "page/sales/false";
            }

        }else if (parse.getState() == 0){
            //查询订单相关
            List<CdyOrder> cdyOrders = cdyOrderService.find(cdyOrder);
            if (cdyOrders.size() > 0){
                CdyOrder c = cdyOrders.get(0);
                //修改库存
                Date date = new Date();

                if (c.getOrderTime().getTime()+ 60*60*1000 < date.getTime()||c.getState() == 2){
                    System.out.println("对不起,超过一小时的商品订单或者该订单已经退货成功,我们不予办理!");
                    return "page/sales/false";
                }else{
                    //修改库存
                    commodity.setId(parse.getCommodityId());
                    //查询到要修改的commodity信息
                    List<Commodity> commodities = commodityService.find(commodity);
                    Commodity co = commodities.get(0);
                    int num =co.getStock() - parse.getNum();
                    System.out.println(commodity+"改库存前");
                    commodity.setStock(num);
                    System.out.println(commodity+"改库存后");
                    commodityService.modify(commodity);

                    //修改订单状态
                    cdyOrder.setState(2);
                    int modify = cdyOrderService.modify(cdyOrder);
                    System.out.println("修改状态成功num="+modify);

                    return "page/sales/ok";
                }
            }else{
                System.out.println("对不起,没有该订单,我们不予办理!");
                return "page/sales/false";
            }
        }
        return "";
    }

    @RequestMapping("choose1")
    public String choose1(HttpSession session, Model model, SalesVo sales,
                          Commodity commodity, Booking booking, Seat seat, CdyOrder cdyOrder) {
        System.out.println("active choose");
        System.out.println(sales);
        System.out.println(model);
        System.out.println(commodity);
        System.out.println(booking);
        System.out.println(cdyOrder);
        System.out.println("---------------------");
        //查询活动相关
        List<Sales> sales1 = salesService.find(sales.parse());
        Sales s = sales1.get(0);
        model.addAttribute("sale",s);
        return "page/sales/choose";
    }



}
